package com.zq;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

/**
 * @ProjectName: ybxy6
 * @Titile: MapDeom
 * @Author: zqsfg
 * @Description: TODO
 */
public class MapDemo {
    public static void main(String[] args) {
//        test0();
//        test1();
//        test2();
        test3();
//        prictice1();
    }
    private static void test0() {
        Map<String,Double> map=new HashMap<>();
        map.put("张三",90.0);
        map.put("李四",80.0);
        map.put("王五",70.0);
        map.put("赵六",70.0);
        System.out.println(map);
        //删除
        map.remove("张三");
        System.out.println(map);
      /*  //清除所有数据
        map.clear();
        System.out.println(map);*/
        //判断集合中是否包含指定的键或者值
        boolean b=map.containsKey("李四");
        System.out.println("b="+b);
        //判断集合中是否包含指定的值
        boolean b1=map.containsValue(90.0);

        System.out.println("b1="+b1);
        //判断集合是否为空
        boolean b2=map.isEmpty();
        System.out.println("b2="+b2);
        //获取集合中元素的个数
        int size=map.size();
        System.out.println("size="+size);
    }

    //键找值
    private static void test1() {
        Map<String,Double> map=new HashMap<>();
        map.put("张三",90.0);
        map.put("李四",80.0);
        map.put("王五",70.0);
        map.put("赵六",70.0);
        //将map中的所有键取出来放在set集合中
        // 获取map中所有的key的集合
        Set<String> keySet = map.keySet();
        //增强for循环
        for (String key : keySet) {
            //根据key获取value
            Double v = map.get(key);
            System.out.println(key+"="+v);
        }
    }

    //键值对
    private static void test2() {
        Map<String,Double> map=new HashMap<>();
        map.put("张三",90.0);
        map.put("李四",80.0);
        map.put("王五",70.0);
        map.put("赵六",70.0);
        //将map集合转换成set集合（将map中的key-vual绑定成一个对象entry）
        Set<Map.Entry<String, Double>> entrySet = map.entrySet();
        //遍历set集合
        for (Map.Entry<String, Double> entry : entrySet) {
            //根据entry获取key和value
            String key = entry.getKey();
            Double value = entry.getValue();
            System.out.println(key+"="+value);

        }
    }
    //lambda表达式
    public static void test3() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 90.0);
        map.put("李四", 80.0);
        map.put("王五", 70.0);
        map.put("赵六", 70.0);
   /*     map.forEach(new BiConsumer<String, Double>() {
            @Override
            public void accept(String key, Double value) {
                System.out.println(key+"="+value);

            }
        });*/
        //简化
        map.forEach((key, value) -> System.out.println(key + "=" + value));


    }
}

  /*  private static void prictice1() {
        Map<String,Double> map=new HashMap<>();
        map.put("张三",90.0);
        map.put("李四",80.0);
        map.put("王五",70.0);
        map.put("赵六",70.0);
        Set<Map.Entry<String, Double>> entrySet = map.entrySet();
        for (Map.Entry<String, Double> entry : entrySet) {
            String key = entry.getKey();
            Double value = entry.getValue();
            System.out.println(key+"="+value);
        }
    }*/
   /* private static void prictice() {
        Map<String,Double> map=new HashMap<>();
        map.put("张三",90.0);
        map.put("李四",80.0);
        map.put("王五",70.0);
        map.put("赵六",70.0);
        map.forEach(new BiConsumer<String, Double>() {
            @Override
            public void accept(String s, Double aDouble) {
                System.out.println(s+"="+aDouble);
            }
        });
        map.forEach((s, aDouble) -> System.out.println(s+"="+aDouble));

    }
}*/
